-- ********************************************
-- * Copyright (c) 2012 TPV R&D Center Brazil *
-- * All Rights Reserved                      *
-- ********************************************

--- toolkit.component.Box

local Box = {}

function Box:new(o)
   if (type(o) ~= 'table') then
      o = {}
   end

   local newBox = {

      area = {
         left = o.left or 0,
         top = o.top or 0,
         width = o.width or 0,
         height = o.height or 0
      },

      left = {},
      top = {},
      right = {},
      bottom = {},

      anchors = {
         left = nil,
         top = nil,
         right = nil,
         bottom = nil,
         leftMargin = 0,
         topMargin = 0,
         rightMargin = 0,
         bottomMargin = 0,

         centerIn = nil,
         fill = nil,

         horizontalCenter = nil,
         verticalCenter = nil
      },

      color = o.color or {0, 0, 0, 0},
      visible = o.visible,
      inFocus = o.inFocus,

      children = {},

      componentType = "Box",

      z = o.z or 1
   }

   if (o.visible == nil) then
      newBox.visible = true
   end

   --update anchors
   newBox.left[1] = newBox.area.left
   newBox.top[1] = newBox.area.top
   newBox.right[1] = newBox.area.left + newBox.area.width
   newBox.bottom[1] = newBox.area.top + newBox.area.height

   --generate an object
   setmetatable(newBox, self)
   self.__index = self

   return newBox
end

function Box:setArea(o)
   if (type(o) == 'table') then
      self.area.left = o.left or self.area.left
      self.area.top = o.top or self.area.top
      self.area.width = o.width or self.area.width
      self.area.height = o.height or self.area.height
      --update anchors
      self.left[1] = self.area.left
      self.top[1] = self.area.top
      self.right[1] = self.area.left + self.area.width
      self.bottom[1] = self.area.top + self.area.height
   end
end

function Box:setAnchors(o)
   if (type(o) == 'table') then
      self.anchors.left = o.left or self.anchors.left
      self.anchors.top = o.top or self.anchors.top
      self.anchors.right = o.right or self.anchors.right
      self.anchors.bottom = o.bottom or self.anchors.bottom
      self.anchors.leftMargin = o.leftMargin or self.anchors.leftMargin
      self.anchors.topMargin = o.topMargin or self.anchors.topMargin
      self.anchors.rightMargin = o.rightMargin or self.anchors.rightMargin
      self.anchors.bottomMargin = o.bottomMargin or self.anchors.bottomMargin
      self.anchors.centerIn = o.centerIn or self.anchors.centerIn
      self.anchors.fill = o.fill or self.anchors.fill
      self.anchors.horizontalCenter = o.horizontalCenter or self.anchors.horizontalCenter
      self.anchors.verticalCenter = o.verticalCenter or self.anchors.verticalCenter
   end
end

function Box:updateArea()
   --check left and right anchors
   if (self.anchors.left) then
      self.area.left = self.anchors.left[1] + self.anchors.leftMargin

      if (self.anchors.right) then
         self.area.width = self.anchors.right[1] - self.area.left - self.anchors.rightMargin
      end

      --update anchors
      self.left[1] = self.area.left
      self.right[1] = self.area.left + self.area.width
   elseif (self.anchors.right) then
      self.area.left = self.anchors.right[1] - self.area.width - self.anchors.rightMargin

      --update anchors
      self.left[1] = self.area.left
      self.right[1] = self.area.left + self.area.width
   elseif (self.anchors.horizontalCenter) then
      self.area.left = (self.anchors.horizontalCenter.area.width / 2) - (self.area.width / 2) + self.anchors.horizontalCenter.area.left

      --update anchors
      self.left[1] = self.area.left
      self.right[1] = self.area.left + self.area.width
   end

   --check top and bottom anchors
   if (self.anchors.top) then
      self.area.top = self.anchors.top[1] + self.anchors.topMargin

      if (self.anchors.bottom) then
         self.area.height = self.anchors.bottom[1] - self.area.top - self.anchors.bottomMargin
      end

      --update anchors
      self.top[1] = self.area.top
      self.bottom[1] = self.area.top + self.area.height
   elseif (self.anchors.bottom) then
      self.area.top = self.anchors.bottom[1] - self.area.height - self.anchors.bottomMargin

      --update anchors
      self.top[1] = self.area.top
      self.bottom[1] = self.area.top + self.area.height
   elseif (self.anchors.verticalCenter) then
      self.area.top = (self.anchors.verticalCenter.area.height / 2) - (self.area.height / 2) + self.anchors.verticalCenter.area.top

      --update anchors
      self.top[1] = self.area.top
      self.bottom[1] = self.area.top + self.area.height
   end

   if (self.anchors.centerIn) then
      self.area.top =   (self.anchors.centerIn.area.height / 2) - (self.area.height / 2) + self.anchors.centerIn.area.top
      self.area.left = (self.anchors.centerIn.area.width / 2) - (self.area.width / 2) + self.anchors.centerIn.area.left

      --update anchors
      self.top[1] = self.area.top
      self.bottom[1] = self.area.top + self.area.height
      self.left[1] = self.area.left
      self.right[1] = self.area.left + self.area.width
   end

   if (self.anchors.fill) then
      self.area.top = self.anchors.fill.area.top
      self.area.left = self.anchors.fill.area.left
      self.area.width = self.anchors.fill.area.width
      self.area.height = self.anchors.fill.area.height

      --update anchors
      self.top[1] = self.area.top
      self.bottom[1] = self.area.top + self.area.height
      self.left[1] = self.area.left
      self.right[1] = self.area.left + self.area.width
   end
end

function Box:setVisible(o)
   if (type(o) ~= 'boolean') then
      o = true
   end

   self.visible = o
   self:redraw()
end

function Box:add(o)
   if (type(o) == 'table') then
      --add components into panel
      for i = 1, #o do
         self.children[#self.children + 1] = {}
         self.children[#self.children] = o[i]
      end
   end
end

return Box
